package web.java.d_Callable;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.junit.Test;

import web.java.z_JDBCUtil.DBUtil;

public class callable {
	
	/**
	 * 调用存储过程，输入参数得到查询结果
	 * */
	@Test
	public void call(){
		Connection conn=DBUtil.connection();
		CallableStatement stmt=null;
		ResultSet result=null;
		try{
			String sql="CALL find_by_id(?)";
			stmt=conn.prepareCall(sql);
			stmt.setInt(1, 4);
			result=stmt.executeQuery();
			while(result.next()){
				System.out.print(result.getInt(1)+"\t");
				System.out.print(result.getString(2)+"\t");
				System.out.print(result.getInt(3));
				System.out.println();
			}
			DBUtil.closeConn(conn, stmt, result);
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
	/*
	 * 输出数据
	 * */
	@Test
	public void callOut(){
		Connection conn=DBUtil.connection();
		CallableStatement stmt=null;
		ResultSet result=null;
		try{
			String sql="CALL name_by_id(?,?)";
			stmt=conn.prepareCall(sql);
			stmt.setInt(1, 4);
			stmt.registerOutParameter(2, java.sql.Types.VARCHAR);
			stmt.executeQuery();
			String name=stmt.getString(2);
			System.out.println(name);
			DBUtil.closeConn(conn, stmt, result);
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
}
